define(function(require) {
	var $ = require("jquery");
	require("res!$UI/system/lib/jquery/jquery-1.11.1.js");
    var Browser = require('$UI/system/lib/base/browser');
	require("$UI/system/lib/cordova/cordova");
    require("cordova!cordova-plugin-camera");
    require("cordova!at.modalog.cordova.plugin.cache");
    require("cordova!cordova-plugin-contacts");
    require("cordova!CallAndMessage");
	var Model = function() {
		this.callParent();
	};
    var showGuidePage=localStorage.getItem('showGuidePage');
    if(showGuidePage=="1"){
        var $indexFrame = $('#iframeid');
        $indexFrame.attr('src',"./index.html"); 
    }else{
        var $indexFrame = $('#iframeid');
        $indexFrame.attr('src',"./guide.html"); 
    }
    document.addEventListener("backbutton", function() {}, false);
	Model.prototype.indexDataReceiverReceive = function(event) {
		var position = event.data;
		var indexPanelId = $(this.getElementByXid("indexPanel")).attr('id');
		var $indexFrame = $('#' + indexPanelId + ' iframe')
		var indexFrame = $indexFrame.get(0);
		$indexFrame.on('load',function(){
			indexFrame.contentWindow.postMessage(position, "*");
		});
	};

    Model.prototype.closeGuidePage = function(event) {
        var indexPanelId = $(this.getElementByXid("indexPanel")).attr('id');
        var $indexFrame = $('#' + indexPanelId + ' iframe')
        $indexFrame.attr('src',"./index.html")
    };
	Model.prototype.cameraCordova = function(callback1,callback2){
         function onSuccess(imageURI) {
            callback1()
            window.resolveLocalFileSystemURI(imageURI, function(entry) {

                    //alert(arr+"|"+imgname+"|"+imgtey+"|"+name);
                entry.file(function(file) {
                    var reader = new FileReader();
                    reader.onloadend = function(e) {
                        callback2(e.target.result)
                    };
                    reader.readAsDataURL(file);
                }, function(e) {
                })
            }, function() {
            });
        }
        function onFail(message) {
        }
        document.addEventListener("deviceready", onDeviceReady, false);        
        function onDeviceReady() {
            navigator.camera.getPicture(onSuccess, onFail, {quality : 50,correctOrientation:1});
        }
    }

    Model.prototype.imageCordova = function(callback1,callback2){
         function onSuccess(imageURI) {
            callback1();
            window.resolveLocalFileSystemURI(imageURI, function(entry) {

                    
                entry.file(function(file) {
                    var reader = new FileReader();
                    reader.onloadend = function(e) {
                        callback2(e.target.result)
                    };
                    reader.readAsDataURL(file);
                }, function(e) {
                })
            }, function() {
            });
        }
        function onFail(message) {

        }
        document.addEventListener("deviceready", onDeviceReady, false);        
        function onDeviceReady() {
            navigator.camera.getPicture(onSuccess, onFail, {quality : 50,sourceType: navigator.camera.PictureSourceType.PHOTOLIBRARY});
        }
    }
    Model.prototype.cacheCordova = function(success_callback,error_callback){
        var success = function(status) {
            success_callback()
        }
        
        var error = function(status) {
            error_callback()
        }
        document.addEventListener("deviceready", onDeviceReady, false);        
        function onDeviceReady() { 
            window.cache.clear(success, error);
        }
    }
    Model.prototype.contactsCordova = function(callback){                      
        document.addEventListener("deviceready", onDeviceReady, false);        
        function onDeviceReady() {   
             var fields = ["name","phoneNumbers"];      
             var options = new ContactFindOptions();
             options.filter = "";
             options.multiple=true;
             navigator.contacts.find(fields, onSuccess, onError,options);
        }       
        function onSuccess(contacts) {
            var contacts_data=[];
            for (var i = 0; i < contacts.length; i++) {
                var data={};
                data.phoneNumbers=contacts[i].phoneNumbers;
                data.name=contacts[i].name;
                contacts_data.push(data)
             }
            callback(contacts_data)
        }
        function onError(contactError) {
        }
    };
    Model.prototype.callCordova = function(callback){                      
        document.addEventListener("deviceready", onDeviceReady, false);        
        function onDeviceReady() {      
            var fields=''
            if (Browser.isX5App) {
                if (Browser.isAndroid) {
                    cordova.plugins.CallAndMessage.GetCallLog(fields, onSuccess, onError);
                }
            }
              
        }       
        function onSuccess(call) {
            callback(call)
        }
        function onError(contactError) {
        }
    };
    Model.prototype.smsCordova = function(callback){                      
        document.addEventListener("deviceready", onDeviceReady, false);        
        function onDeviceReady() {   
            var fields=''
            if (Browser.isX5App) {
                if (Browser.isAndroid) {
                    cordova.plugins.CallAndMessage.GetMessageLog(fields, onSuccess, onError);  
                }
            }
            
        }       
        function onSuccess(sms) {
            callback(sms)
        }
        function onError(smsError) {
        }
    };
	return Model;
});